# 对象库层
# 使用单元测试框架所组织的测试脚本中存在大量的元素定位代码,一旦界面元素发生变化
# 所有用到该元素的地方都不得不全部进行修改，脚本对页面元素信息非常依赖。且定位元素代码大量冗余
import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select

from base.admin.base_page import BasePage


class GoodPage(BasePage):

    def __init__(self):
        super().__init__()
        # iframe
        self.frame = (By.ID, "workspace")
        # 添加商品
        self.add_goods = (By.CSS_SELECTOR, "[title='添加商品']")
        # 商品名称
        self.goods_name = (By.NAME, "goods_name")
        # 一级分类
        self.o_cate = (By.ID, "cat_id")
        # 二级分类
        self.t_cate = (By.ID, "cat_id_2")
        # 三级分类
        self.th_cate = (By.ID, "cat_id_3")
        # 商品品牌
        self.brand = (By.ID, "brand_id")
        # 本店售价 selling price
        self.s_price = (By.NAME, "shop_price")
        # 市场价
        self.m_price = (By.NAME, "market_price")
        # 成本价
        self.c_price = (By.NAME, "cost_price")
        # 确认提交
        self.comfirm_btn = (By.CSS_SELECTOR, ".ncap-btn-big")

    # 找iframe
    def find_frame(self):
        return self.fd_em(self.frame)

    # 找添加商品按钮
    def find_add_goods(self):
        return self.fd_em(self.add_goods)

    # 找商品名称
    def find_goods_name(self):
        return self.fd_em(self.goods_name)

    # 找一级分类
    def find_o_cate(self):
        return self.fd_em(self.o_cate)

    # 找二级分类
    def find_t_cate(self):
        return self.fd_em(self.t_cate)

    # 找三级分类
    def find_th_cate(self):
        return self.fd_em(self.th_cate)

    # 找商品品牌
    def find_brand(self):
        return self.fd_em(self.brand)

    # 找本店售价
    def find_s_price(self):
        return self.fd_em(self.s_price)

    # 找市场价
    def find_m_price(self):
        return self.fd_em(self.m_price)

    # 找成本价
    def find_c_price(self):
        return self.fd_em(self.c_price)

    # 找到确认按钮
    def find_comfirm_btn(self):
        return self.fd_em(self.comfirm_btn)


# 操作层
class GoodsHandle(BasePage):

    def __init__(self):
        super().__init__()
        # 创建对象库层的实例对象,方便调用实例方法
        self.goods_page = GoodPage()

    # 点击【新增商品】
    def click_add_goods(self):
        # 1.切换iframe
        self.driver.switch_to.frame(self.goods_page.find_frame())
        # 2.点击新增商品
        self.goods_page.find_add_goods().click()

    # 输入商品名称
    def input_goods_name(self, g_name):
        self.type_ele(self.goods_page.find_goods_name(), g_name)

    # 选择商品分类
    def check_category(self, o_cate, t_cate, th_cate):
        # 选择1级分类
        Select(self.goods_page.find_o_cate()).select_by_value(o_cate)
        # 点击2级分类
        self.goods_page.find_t_cate().click()
        # 选择2级分类
        time.sleep(3)
        Select(self.goods_page.find_t_cate()).select_by_value(t_cate)
        # 点击3级分类
        self.goods_page.find_th_cate().click()
        time.sleep(3)
        # 选择3级分类
        Select(self.goods_page.find_th_cate()).select_by_value(th_cate)

    # 选择品牌
    def check_brand(self, brand):
        Select(self.goods_page.find_brand()).select_by_value(brand)

    # 输入本店售价
    def input_s_price(self, s_price):
        self.type_ele(self.goods_page.find_s_price(), s_price)

    # 输入市场价
    def input_m_price(self, m_price):
        self.type_ele(self.goods_page.find_m_price(), m_price)

    # 输入成本价
    def input_c_price(self, c_price):
        self.type_ele(self.goods_page.find_c_price(), c_price)

    # 点击【确认提交】
    def click_comfirm_btn(self):
        self.goods_page.find_comfirm_btn().click()


# 业务层
class GoodsProxy:

    def __init__(self):
        # self.goods_handle存储了操作层的实例对象
        self.goods_handle = GoodsHandle()

    # 新增商品
    def add_goods(self, g_name, o_cate, t_cate, th_cate, b_name, s_price, m_price, c_price):
        """
        g_name:商品名称
        o_cate:一级分类
        t_cate:二级分类
        th_cate:三级分类
        b_name:品牌名称
        s_price:本店售价
        m_price:市场价
        c_price:成本价
        """
        # 2.点击新增商品
        self.goods_handle.click_add_goods()
        # 3.输入商品名称
        self.goods_handle.input_goods_name(g_name)
        # 4.选择商品分类
        self.goods_handle.check_category(o_cate, t_cate, th_cate)
        # 5.选择品牌
        self.goods_handle.check_brand(b_name)
        # 6.输入本店售价
        self.goods_handle.input_s_price(s_price)
        # 7.输入市场价
        self.goods_handle.input_m_price(m_price)
        # 8.输入成本价
        self.goods_handle.input_c_price(c_price)
        # 9.点击【确认提交】
        self.goods_handle.click_comfirm_btn()
